package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.CheckRecord;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.CheckRecordService;
import org.kjtc.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CheckRecordController extends BaseController{

	@Resource(name = "checkRecordService")
	private CheckRecordService checkRecordService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value = "/searchCheckRecord")
	@ResponseBody
	public Map<String, Object> searchCheckRecord(CheckRecord checkRecord, String page, String rows){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录查询开始");
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		// 获取所有设备数据
		List<CheckRecord> list = null;
		try {
			list = checkRecordService.getCheckRecordPage(checkRecord, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("checkRecordList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "检查记录查询");
		checkRecord = null;
		return map;
	}

	/**
	 * 增加检查记录
	 */
	@ResponseBody
	@RequestMapping(value="/saveCheckRecord")
	public Map<String, Object> saveBatteryType(CheckRecord checkRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录添加开始");
		checkRecord.setCheckTime(Tools.str2DateTime(checkRecord.getCheckDate()));
		checkRecord.setCreateUser(this.getUesr().getUserID());
		checkRecord.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = checkRecordService.insertCheckRecord(checkRecord);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录添加异常:" + e.getMessage());
		}
        logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "检查记录添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "检查记录添加");
		checkRecord = null;
		return map;
	}
}
